<template>
	<view class="container">
		<view :class="fixTab ? 'tab-fixed' :'tab'">
			<van-tab :tabs="tabs" :underlineWidth="underlineWidth" @tabClick="tabClick" :currentActive="currentActive" />
		</view>
		<view class="content" v-if="order.length > 0">
			<view class="order-box" v-for="(item,index) in order" :key="index">
			<!-- 	<view class="receive-date">
					{{item.takeGoodsDate}}提货
				</view> -->
				<view class="order-list">
					<view class="order-item">
						<view class="receiver">
							<view class="left">
								<text class="name" >{{item.activityName}}</text>
								<!-- {{item.mobile}} -->
								<!-- <image @tap="contactMerchantFun(item.mobile)"  mode=""></image> -->
							</view>
							<text class="yellow1" v-if="item.state==1">{{item.state | statusFilter }}</text>
							<text class="yellow2" v-if="item.state==2">{{item.state | statusFilter }}</text>
							<!-- <text class="yellow" v-if="idx==2" v-for="(obj,index) in item.selOrderGoodsList" :key="index">{{obj.orderState | statusFilter }}</text> -->
						</view>
						<view class="goods-box" >
							<view class="goods"  @tap="getDetail(item)">
								<view style="display: flex;width:67%;overflow:hidden;">
									<view class="img-list" v-for="(obj,index) in item.itemModelList" :key="index">
										<image :src="imageUrl + obj.toolingImage" mode=""></image>
										<text class="mask" v-if="obj.refundState==3">已退款</text>	
									</view>
								</view>
								<view class="number" >								
									<text class="more1">共{{item.goodsNum}}件</text>
									<image class="more" src="/static/img/next.png" mode=""></image>
								</view>
							</view>
							<view class="money">
								<view>订单合计：<text class="red">￥{{item.money}}</text></view>
								
							</view>
							<view class="handle">
								<view v-if="item.createdTime">创建时间：{{item.createdTime.slice(0, 16)}}</view>
							
								<!-- <view class="btn" v-if="(item.orderState == 3 && item.refundState == 0 && item.isRefuse != 1 )" @tap="confirmReceive(item.id)">确认提货</view>
								<view class="btn" v-if="item.orderState > 2 && item.orderState < 5 && item.refundState == 0" @tap="applyRefundItem(item.id)">申请退款</view> -->
								<view class="pay-btn" v-if="item.state == 0" @tap="pay(item)">去付款</view>
								<view class="del-btn" v-if="item.state == 1" @tap="takeItem(item.id,'')">确认领取</view>
								<!-- <view class="take-btn" v-if="item.state == 2" @tap="(item.id)">确认提货</view> -->
								<view class="del-btn" v-if="item.state == 1"  @tap="deleteItem(item.id,'')">删除</view>
							</view>
							<view class="status-icon">
								<image :src="imageUrl +'/youxuan/close-icon.png'" mode="" v-if="item.orderState==6"></image>
								<image :src="imageUrl +'/youxuan/finished.png'" mode="" v-if="item.orderState==5"></image>
								<image :src="imageUrl +'/youxuan/close-icon.png'" mode="" v-for="(obj,index) in item.selOrderGoodsList" :key="index" v-if="obj.orderState==6 && idx==2"></image>
								<image :src="imageUrl +'/youxuan/finished.png'" mode="" v-for="(obj,index) in item.selOrderGoodsList" :key="index" v-if="obj.orderState==5 && idx==2"></image>
							</view>
						</view>
					</view>
				</view>
				
			</view>
		</view>
		<view class="no-data" v-else>
			<image :src="imageUrl+'/tuanzhang/nodata.png'" mode=""></image>
			<view>暂无数据</view>
		</view>
		<!-- 电话 -->
		<contact-merchant  :phoneTitle="telephoneTitle" :phone="contactMerchant" v-if="contactMerchantShow" @closerMerchantPhone="closerMerchantPhoneFun"></contact-merchant>
		<!-- 申请退款 -->
		<apply-refund  :orderDetail="orderDetail" :orderGoods="orderGoods" v-if="refundShow" @closerRefund="closerRefundFun" @confirmRefund="confirmRefund"></apply-refund>
	</view>
</template>

<!-- <script module="filter" lang="wxs" src="../../filter/filter.wxs"></script> -->

<script>
	import vanTab from "../../components/van-tab/van-tab";
	import contactMerchant from "../../components/contact-merchant/index";
	import applyRefund from "../../components/apply-refund/index";
	var timer = null;
	const app = getApp();

	export default {
		data() {
			return {
				imageUrl: 'http://124.71.202.247:8080/image/',
				fixTab:false,
				currentActive:0,
				tabs:["待收货","已完成","全部"],
				underlineWidth: 36,
				scrollTop:0,// 滚动条高度
				pagination: {
					pageNum: 1,
					pageSize: app.globalData.paginationLimit
				},
				order:[],
				pageAdd: true,// 上滑时控制添加页数
				clearList:false,//清空列表
				noListData: false,// 显示没有数据提示
				shopId:'',
				contactMerchantShow:false,
				contactMerchant:'',
				telephoneTitle:'收货人电话',
				refundShow:false,
				orderDetail:{},
				orderGoods:{},
				idx:0,
				countdown:'',
			};
		},

		components: {
			vanTab,
			contactMerchant,
			applyRefund
			
		},
	filters:{
		statusFilter(type) {
			switch (type) {
				case 0:
					return '待付款';
					break;
				case 1:
					return '待领取';
					break;
				case 2:
					return '已领取';
					break;
				case 3:
					return '待提货';
					break;
				case 4:
					return '已收货';
					break;
				case 5:
					return '已完成';
					break;
				case 6:
					return '超时后关闭';
					break;
				case 9:
					return '已取消';
					break;
			}
		},
		refundStatusFilter(type) {
			switch (type) {
				case 0:
					return '无退款';
					break;
				case 1:
					return '待审核';
					break;
				case 2:
					return '通过';
					break;
				case 3:
					return '完成退款';
					break;
				case 9:
					return '拒绝';
					break;
			}
		},
	},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function (options) {
			// this.userId = app.globalData.userID;
			this.orderType = options.orderType;
			console.log('类型',this.orderType)
			this.currentActive = this.orderType;
			// this.getOrderList(this.currentActive);
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function () {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
			this.order=[];
			this.getOrderList(this.currentActive)
			// this.shopId = app.globalData.shopInfo.id || '';
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function () {
			uni.showNavigationBarLoading();
			uni.showNavigationBarLoading();
			this.clearList = true;
			// 订单页数
			this.noListData = false;
			// 没有数据
			this.pagination = {
				page: 1,
				limit: app.globalData.paginationLimit
			};
			uni.showLoading({
				title: '加载中'
			});
			console.log('当前tab index>>>>',this.currentActive)
			this.getOrderList(this.currentActive)
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
	onReachBottom: function () {
		console.log('上拉触底',this.pageAdd)
		if (this.pageAdd) {
			if (!this.noListData) {
				let pagination = this.pagination;
				pagination.page = pagination.page + 1;
				
				this.pageAdd = false;
				this.pagination = pagination;
				
				uni.showLoading({
					title: '加载中'
				});
				this.getOrderList(this.currentActive);
				uni.hideLoading();
			}
				
		}
	},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function (res) {
		// 	if (res.from === 'button') {}
		
		// 	return getApp().globalData.shareApp();
		},
		methods: {
			onPageScroll: function (e) {
				const _this = this;
				let srolHeight = e.scrollTop;
				this.scrollTop = srolHeight;
				if(this.scrollTop >= 6){
					this.fixTab = true;
				}else{
					this.fixTab = false;
				}
			},
			getOrderList(idx){
				console.log('idx',idx)
				this.idx=idx;
				let that = this;
				// this.order = [];
				let params={};
				if(idx == 0){
					params={
						pageNum : 1,
						pageSize:100,
						orderState:1
					}
				}else if(idx == 1){
					params={
						pageNum : 1,
						pageSize:100,
						orderState:2
					}
				}else{
					params={
						pageNum : 1,
						pageSize:100,
						orderState:''
					}
				}
				
					let _this =this;
					_this.sendRequest({
						url : "/order/list",
						method : "GET",
						data :params,
						hideLoading : true,
						success:function (res) {
							console.log(res)
							_this.order=res.records
						}
					})
				
			},
			
			//去支付
			pay(row){
				console.log(row);
				uni.navigateTo({
					url: `/packagesBusinessSuper/pages/payOrder/payOrder?orderID=${row.id}&payFee=${row.payFee}&createdTime=${row.createdTime}`
				})
			},
			// 取消某个商品/整个订单
			cancelItem(orderNo,goodsId){
				let params={
					orderNo: orderNo,
					goodsId: goodsId
				}
				console.log(params)
				uni.showModal({
					title: '提示',
					content: '确认取消？',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							businessSuperApi.cancelOrder(params).then(res=>{
								console.log('取消res=>',res)
								if(res.status == "SUCCEED"){
									uni.showToast({
										title: '取消成功',
										icon: 'none',
										duration: 2000
									})
									this.getOrderList(this.currentActive)
								}else{
									uni.showToast({
										title: res.errorMessage,
										icon: 'none',
										duration: 2000
									})
								}
							})
						}
					}
				});
			},
			// 删除某个商品
			deleteItem(orderNo,goodsId){
				let that = this;
				let params={
					orderNo: orderNo,
					goodsId: goodsId
				}
				console.log(params)
				uni.showModal({
					title: '提示',
					content: '确认删除？',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							that.sendRequest({
								url :"/order/"+orderNo,
								method : "DELETE",
								success:function (res) {
									console.log('删除res=>',res)
									if(res.status == "SUCCEED"){
										uni.showToast({
											title: '删除成功',
											icon: 'none',
											duration: 2000
										})
											that.$nextTick(function(){
												that.order = [];
												that.pagination.page=1;
												that.getOrderList(that.currentActive)
											})	
									}else{
										setTimeout(() => {
										uni.showToast({
											title: res.errorMessage,
											icon: 'none',
											duration: 4000
										})
										},500)
									}
								}
							})
						}
					}
				})
			},
			// 确认提货
			takeItem(rowId){
				console.log(rowId)
				var that=this;
				uni.showModal({
					title: '确认已领取工装吗？',
					// content: '',
					confirmText: '确认',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							// foodApi.orderfetch(rowId).then(res=>{
								that.sendRequest({
								url :"/order/fetch/"+rowId,
								method : "PUT",
								// data :phone,
								hideLoading : true,
								success:function (res) {
								// if(res.status == "SUCCEED"){
									uni.showToast({
										title: '提交成功',
										icon: 'none',
										duration: 2000
									})
									that.order = [];
									that.pagination.page=1;
									that.getOrderList(that.currentActive);
								// }else{
								// 	uni.showToast({
								// 		title: res.errorMessage,
								// 		icon: 'none',
								// 		duration: 2000
								// 	})
								}
							})
						}
					}
				});
			},
			// 确认提货
			// confirmReceive(orderNo){
			// 	let params={
			// 		orderNo :orderNo,
			// 		warn: 0
			// 	}
			// 	uni.showModal({
			// 		title: '提示',
			// 		content: `确认收货？`,
			// 		confirmText: '确认',
			// 		cancelText: '取消',
			// 		success: res => {
			// 			if (res.confirm) {
			// 				businessSuperApi.receiveOrder(row.id).then(res=>{
			// 					if(res.status == "SUCCEED"){
			// 						uni.showToast({
			// 							title: '提交成功',
			// 							icon: 'none',
			// 							duration: 2000
			// 						})
			// 						this.getOrderList(this.currentActive);
			// 					}else{
			// 						uni.showToast({
			// 							title: res.errorMessage,
			// 							icon: 'none',
			// 							duration: 2000
			// 						})
			// 					}
			// 				})
			// 			}
			// 		}
			// 	});
			// },
			// 申请退款
			// applyRefundItem(id){
			// 	orderApi.getOrderDetails(id).then(res=>{
			// 		if(res.status == "SUCCEED"){
			// 			this.orderDetail = res.result;
			// 			this.orderGoods = res.result.selOrderGoodsList;
			// 			this.orderGoods.forEach(v=>{
			// 				// 未选择
			// 				this.$set(v,'check',false)
			// 			})
			// 			this.refundShow = true;
			// 		}
			// 	})
			// },
			// 关闭申请退款窗口
			closerRefundFun(){
				this.refundShow = false
			},
			// 确认退款
			confirmRefund: function (e) {
				console.log(e);
				uni.navigateTo({
					url:`../refund/index?refundData=${JSON.stringify(e)}&orderDetail=${JSON.stringify(this.orderDetail)}`
				})
			},
			// 关闭打电话窗口
			closerMerchantPhoneFun: function() {
				this.contactMerchantShow = false;
			},
			// 联系收货人
			// contactMerchantFun: function(phone) {
			// 	this.contactMerchantShow = true;
			// 	this.contactMerchant = phone;
			// },
			tabClick(data){
				this.order = [];
				this.currentActive = data.index;
				this.getOrderList(this.currentActive)
			},
		getDetail(row){
			console.log("row",row)
				uni.setStorageSync('dingdansj', JSON.stringify(row));
			
				uni.navigateTo({
					url:`../refundOrderDetail/refundOrderDetail`
				})
			
			
		},
		}
	};
</script>
<style>
	@import "./order.css";
</style>
